package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.pojo.AddressBook;
import com.itheima.pojo.R;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public R list() {
        log.info("查询该用户地址...");
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);

        List<AddressBook> addressBookList = addressBookService.list(lqw);

        return R.success(addressBookList);
    }

    @PostMapping()

    public R addAddress(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("添加地址簿");
        addressBookService.save(addressBook);
        return R.success("添加地址簿成功!");
    }

    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook) {
        log.info("设置默认值");
        long userId = BaseContext.getCurrentId();
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();

        luw.eq(AddressBook::getUserId,userId);
        luw.set(AddressBook::getIsDefault,0);
        addressBookService.update(luw);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
//        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(AddressBook::getUserId,addressBook.getUserId());
//
//        List<AddressBook> addressBookList = addressBookService.list(lqw);

        return R.success(addressBook);
    }

    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id){
        log.info("根据id查询地址");
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        log.info("修改地址簿");
        addressBookService.updateById(addressBook);
        return R.success("修改成功!");
    }

    @DeleteMapping
    public R deleteById(Long ids){
        log.info("根据id删除地址");
        addressBookService.removeById(ids);
        return R.success("删除成功!");
    }

    @GetMapping("/default")
    public R getDefault(){
        log.info("查询默认地址");
        long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,userId);
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(lqw);
        if(addressBook==null){
            return R.error("没有查到该对象");
        }else{
            return R.success(addressBook);
        }
    }

}
